package com.derbysoft.dswitch.adapter.ihg.web.action.admin.setting.config;

import com.derbysoft.dswitch.adapter.ihg.common.domain.ConfigInfo;
import com.derbysoft.dswitch.adapter.ihg.common.repository.ConfigRepository;
import com.derbysoft.dswitch.adapter.ihg.common.utils.Constants;
import com.derbysoft.dswitch.adapter.ihg.web.action.support.PaginateActionSupport;
import com.derbysoft.dswitch.adapter.ihg.web.model.config.AdditionalDetailCode;
import com.derbysoft.dswitch.adapter.ihg.web.model.config.Configuration;
import com.derbysoft.dswitch.adapter.ihg.web.model.config.MatchPriorityConfig;
import com.derbysoft.dswitch.adapter.ihg.web.model.config.RoomTypeName;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;


public class ListAction extends PaginateActionSupport {

    @Autowired
    private ConfigRepository configRepository;

    private Configuration configuration;

    @Action("list")
    public String list() {
        createConfiguration();
        return SUCCESS;
    }

    private void createConfiguration() {
        configuration = new Configuration();
        configuration.setRoomTypeName(createRoomTypeName());
        configuration.setAdditionalDetailCode(createAdditionalDetailCode());
        configuration.setMatchPriorityConfig(createMatchPriorityConfig());
    }

    private MatchPriorityConfig createMatchPriorityConfig() {
        MatchPriorityConfig matchPriorityConfig = new MatchPriorityConfig();
        ConfigInfo configInfo = configRepository.loadByCode(Constants.KEY_MATCH_PRIORITY);
        matchPriorityConfig.setName(configInfo.getName());
        matchPriorityConfig.setValue(configInfo.getValue());
        matchPriorityConfig.setDescription(configInfo.getDescription());
        matchPriorityConfig.setUsable(configInfo.isUsable());
        return matchPriorityConfig;
    }

    private AdditionalDetailCode createAdditionalDetailCode() {
        AdditionalDetailCode additionalDetailCode = new AdditionalDetailCode();
        ConfigInfo configInfo = configRepository.loadByCode(Constants.KEY_ADDITIONAL_DETAIL_CODE);
        additionalDetailCode.setName(configInfo.getName());
        additionalDetailCode.setValue(configInfo.getValue());
        additionalDetailCode.setDescription(configInfo.getDescription());
        additionalDetailCode.setUsable(configInfo.isUsable());
        return additionalDetailCode;
    }

    private RoomTypeName createRoomTypeName() {
        RoomTypeName roomTypeName = new RoomTypeName();
        ConfigInfo configInfo = configRepository.loadByCode(Constants.KEY_ROOM_TYPE_NAME_FIELD);
        roomTypeName.setName(configInfo.getName());
        roomTypeName.setValue(configInfo.getValue());
        roomTypeName.setDescription(configInfo.getDescription());
        roomTypeName.setUsable(configInfo.isUsable());
        return roomTypeName;
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }
}
